package com.hnust.root.service.impl.forward.wx;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnust.root.common.util.ComUtil;
import com.hnust.root.dao.entity.domain.forward.WxUserInfo;
import com.hnust.root.dao.mapper.wx.WxUserInfoMapper;
import com.hnust.root.service.forword.wx.TopicService;
import com.hnust.root.service.forword.wx.WxUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class WxUserInfoServiceImp extends ServiceImpl<WxUserInfoMapper,WxUserInfo> implements WxUserInfoService {
    private static final Log LOGGER = LogFactory.get();

    @Autowired
    private WxUserInfoMapper userInfoMapper;

    @Autowired
    private TopicService topicService;

    //根据openId获取用户的详细信息
    @Override
    public WxUserInfo getUserInfoByOpenId(String openId) {
        WxUserInfo userInfo =  userInfoMapper.selectOne(new QueryWrapper<WxUserInfo>().eq("open_id", openId));
        if (userInfo != null) {
            userInfo.setTopicCount(topicService.getTopicListByUserId(openId).size());
        }
        return userInfo;
    }

    //更新用户信息
    @Override
    public synchronized boolean updateUserInfo(String openId,String key,String value) {
        if (!StrUtil.isEmpty(openId)) {
            WxUserInfo wxUserInfo = userInfoMapper.selectOne(new QueryWrapper<WxUserInfo>().eq("open_id", openId));
            if (wxUserInfo == null) {
                LOGGER.info("用户openId为： {}  没有详细信息，为他生成一条");
                wxUserInfo = new WxUserInfo();
                wxUserInfo.setOpenId(openId);
                userInfoMapper.insert(wxUserInfo);
            }
            return ComUtil.DBisSuccess(userInfoMapper.updateUserInfoByKey(openId, key, value));
        }
        return false;
    }

    @Override
    public List<WxUserInfo> getUserInfoByUniversity(String university) {
        return userInfoMapper.selectList(new QueryWrapper<WxUserInfo>().eq("university", university));
    }

    @Override
    public boolean updateUser(WxUserInfo wxUserInfo,String openId) {
        boolean res = false;
        if (!StrUtil.isEmpty(openId)) {
            WxUserInfo userInfo = getUserInfoByOpenId(openId);
            if (userInfo == null) {
                userInfo = new WxUserInfo();
                userInfo.setOpenId(openId);
                userInfoMapper.insert(userInfo);
            }
            res =  ComUtil.DBisSuccess(userInfoMapper.update(wxUserInfo, new QueryWrapper<WxUserInfo>().eq("open_id",openId)));
        }
        return res;
    }

    /**
     * 获取我的校友
     * @param openId
     * @return
     */
    @Override
    public List<WxUserInfo> getSchoolMates(String openId) {
        WxUserInfo userInfo = getUserInfoByOpenId(openId);
        String university = userInfo.getUniversity();
        List<WxUserInfo> userInfoList;
        if (!StrUtil.isEmpty(university)) {
            // TODO： 过滤掉自己
            userInfoList = getUserInfoByUniversity(university);
        } else {
            userInfoList = Collections.EMPTY_LIST;
        }
        return userInfoList;
    }
}
